package com.patent.service.impl;

import com.patent.common.CreateOrderNum;
import com.patent.common.PatentConstant;
import com.patent.common.ResultCode;
import com.patent.common.ResultData;
import com.patent.dao.RegisterRecordMapper;
import com.patent.domain.po.RegisterRecord;
import com.patent.domain.vo.RegisterInfoResponseVo;
import com.patent.service.InvoiceAmountService;
import com.patent.service.RegisterRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("registerRecordService")
@Transactional
public class RegisterRecordServiceImpl implements RegisterRecordService {

	private static final Logger log = LoggerFactory.getLogger(RegisterRecordServiceImpl.class);

	@Resource(name="registerRecordMapper")
	private RegisterRecordMapper registerRecordMapper;

	@Resource(name="invoiceAmountService")
	private InvoiceAmountService invoiceAmountService;

	@Override
	public ResultData addRegisterInfo(String userMobile, String patentId, Integer registerNum) {
		ResultData resultData = new ResultData();
		RegisterRecord registerRecord = new RegisterRecord();
		registerRecord.setOrderNo(PatentConstant.REGISTER_ORDER_FLAG + CreateOrderNum.getOrderNo());
		registerRecord.setPatentId(Integer.parseInt(patentId));
		registerRecord.setUserMobile(userMobile);
		registerRecord.setCreateDate(new Date());
		registerRecord.setValidFlag(PatentConstant.YES_FLAG);
		registerRecord.setOrderState(PatentConstant.NO_PAY);
		registerRecord.setRegisterNum(registerNum);
		//算价格
		registerRecord.setOfficialAmount(new BigDecimal(30).multiply(new BigDecimal(registerNum)).doubleValue());
		registerRecord.setAgentAmount(new BigDecimal(100).multiply(new BigDecimal(registerNum)).doubleValue());
		registerRecord.setOrderAmount(new BigDecimal(130).multiply(new BigDecimal(registerNum)).doubleValue());
		registerRecordMapper.insertSelective(registerRecord);
		resultData.setCode(ResultCode.success.getCode());
		resultData.setMsg(ResultCode.success.getMessage());
		resultData.setResult(registerRecord.getOrderNo());
		return resultData;
	}

	@Override
	public ResultData queryRegisterInfo(String userMobile, String queryFlag, Integer page, Integer rows) {
		ResultData resultData = new ResultData();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userMobile",userMobile);
		map.put("page",(page-1)*rows);
		map.put("rows",rows);
		map.put("queryFlag",queryFlag);
		List<RegisterInfoResponseVo> list = registerRecordMapper.queryRegisterInfo(map);
		resultData.setCode(ResultCode.success.getCode());
		resultData.setMsg(ResultCode.success.getMessage());
		resultData.setResult(list);
		return resultData;
	}

	@Override
	public RegisterRecord selectByPrimaryKey(Integer id) throws Exception{
		return registerRecordMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateByPrimaryKeySelective(RegisterRecord record) {
		registerRecordMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 通过订单号查登记录副本
	 */
	@Override
	public RegisterRecord queryByOrderNo(String orderNo) {
		return registerRecordMapper.queryByOrderNo(orderNo);
	}

	@Override
	public void updateRecordR(RegisterRecord registerRecord) {
		try {
			updateByPrimaryKeySelective(registerRecord);
			invoiceAmountService.dealInvoiceAmount("add", registerRecord.getUserMobile(), registerRecord.getOrderAmount());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("修改登记录副本失败：" + e.getMessage());
			throw new RuntimeException(e);
		}
	}

}
